<template>
  <div class="login-container">
    <div class="login-box">
      <el-form ref="loginFormref" :model="loginForm" :rules="loginFormRules">
        <img src="./logo_index.png" alt />
        <el-form-item prop="mobile">
          <el-input v-model="loginForm.mobile" placeholder="请输入手机号码">
                <i slot="prefix" class="iconfont icon-shouji"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="loginForm.code" placeholder="请输入校验码">
                <i slot="prefix" class="iconfont icon-ziyuan"></i>
          </el-input>
        </el-form-item>
        <el-form-item style=" text-align: left" prop="xieyi">
          <el-checkbox v-model="loginForm.xieyi" style=" margin-right: 10px;"></el-checkbox>
          <span>我已阅读并同意用户协议和隐私条款</span>
        </el-form-item>
        <el-form-item>
          <el-button :loading="isactive" :disabled="isactive" type="primary" style="width:100%" @click="login()">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 引入极验的
import '@/assets/js/gt.js'
import '@/assets/iconfont/iconfont.css'
export default {
  data () {
    var xieyiText = (rule, value, callback) => {
      //   if (!value) {
      //     callback(new Error('请勾选'))
      //   } else {
      //     callback()
      //   }
      //   升级三元表达式
      value ? callback() : callback(new Error('请勾选已经阅读'))
    }
    return {
      loginForm: {
        mobile: '15324716671',
        code: '246810', // 验证码
        xieyi: true

      },
      isactive: false,
      ctaObj: null, // 极验窗口对象
      //   校验规则
      loginFormRules: {
        mobile: [
          { required: true, message: '手机号码必填', trigger: 'blur' },
          {
            pattern: /^1[35789]\d{9}$/,
            message: '格式不对请输入11位数字'

          }
        ],
        code: [{ required: true, message: '校验码必填' }],
        // 自定义校验
        xieyi: [{ validator: xieyiText }]
      }
    }
  },
  methods: {
    login () {
      // 整个表单验证
      this.$refs.loginFormref.validate(valid => {
        if (valid) {
          if (this.ctaObj !== null) {
            return this.ctaObj.verify()
          }
          // 进行人机交互验证
          // 设置loading显示
          this.isactive = true
          let pro = this.$http.get(`/captchas/${this.loginForm.mobile}`)
          pro
            .then((result) => {
            // console.log(res)
            // 生成极验验证窗口
            // 请检测data的数据结构， 保证data.gt, data.challenge, data.success有值
              var { data } = result.data
              window.initGeetest({
              // 以下配置参数来自服务端 SDK

                gt: data.gt,
                challenge: data.challenge,
                offline: !data.success,
                new_captcha: true,
                product: 'bind' // 没有按钮，通过登录按钮激活验证
              }, (captchaObj) => {
              // 这里可以调用验证实例 captchaObj 的实例方法
                captchaObj.onReady(() => {
                // 验证码ready之后才能调用verify方法显示验证码
                // 生成窗口
                  captchaObj.verify()
                  // 把窗口对象赋予给ctaObj对象
                  this.ctaObj = captchaObj
                  // 生成窗口以后让loading图不再转圈圈
                  this.isactive = false
                }).onSuccess(() => {
                // 人的行为正确
                  this.loginAct()
                }).onError(() => {
                // your code
                })
              })
            })
            .catch(err => {
              return this.$message.error('获得极验初始校验信息错误：' + err)
            })
        }
      })
    },
    // 账号真实性校验
    loginAct () {
      let pro = this.$http.post('/authorizations', this.loginForm)
      console.log(pro)

      pro.then(result => {
        console.log(result)

        if (result.data.message === 'OK') {
          window.sessionStorage.setItem('userinfo', JSON.stringify(result.data.data))
          this.$router.push('/home')
        }
      })
        .catch(err => {
          this.$message({ type: 'error', message: '错了哦，这是一条错误消息' + err, duration: 1000 })
        })
    }

  }
}
</script>

<style lang="less" scoped>
.login-container {
  height: 100%;
  background-color: #eee;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url("./login_bg.jpg");
  background-size: cover;
  .login-box {
    width: 410px;
    height: 350px;
    background-color: white;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    img {
      width: 50%;
    }
    .el-form {
      width: 75%;
    }
  }
}

</style>
